home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v2.1 / Amiga Developer CD v2.1.iso / Contributions / Haage_&_Partner / Storm-Projects / NDKExamples1 / intuition / nlmenu.c < prev    next >
C/C++ Source or Header  |  1999-04-16  |  8KB  |  345 lines

  1. /*
  2.  * nlmenu.c - shows use of NewLook menus using Intuition and GadTools
  3.  *
  4.  * (c) Copyright 1992-1996 Amiga International, Inc. All rights reserved.
  5.  *
  6.  * This software is provided as-is and is subject to change; no warranties
  7.  * are made.  All use is at your own risk.  No liability or responsibility
  8.  * is assumed.
  9.  *
  10.  * Demo shows off the new look menu features of V39.
  11.  */
  12.  
  13. #include <exec/types.h>
  14. #include <intuition/intuitionbase.h>
  15. #include <intuition/gadgetclass.h>
  16. #include <intuition/imageclass.h>
  17. #include <libraries/gadtools.h>
  18. #include <graphics/gfxbase.h>
  19.  
  20. #include <clib/exec_protos.h>
  21. #include <clib/graphics_protos.h>
  22. #include <clib/intuition_protos.h>
  23. #include <clib/diskfont_protos.h>
  24. #include <clib/gadtools_protos.h>
  25. #include <clib/dos_protos.h>
  26.  
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29.  
  30. /*------------------------------------------------------------------------*/
  31.  
  32. void main(int, char *[]);
  33. void bail_out(int);
  34. BOOL HandleMenuEvent(UWORD);
  35.  
  36. /*------------------------------------------------------------------------*/
  37.  
  38. /* Here we specify what we want our menus to contain: */
  39.  
  40. struct NewMenu mynewmenu[] =
  41. {
  42.     { NM_TITLE, "Project",      0 , 0, 0, 0,},
  43.     {  NM_ITEM, "Open...",     "O", 0, 0, 0,},
  44.     {  NM_ITEM, "Save",      0 , 0, 0, 0,},
  45.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  46.     {  NM_ITEM, "Print",      0 , 0, 0, 0,},
  47.     {   NM_SUB, "Draft",      0 , CHECKIT|CHECKED, ~1, 0,},
  48.     {   NM_SUB, "NLQ",      0 , CHECKIT, ~2, 0,},
  49.     {   NM_SUB, "Laser",      0 , CHECKIT, ~4, 0,},
  50.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  51.     {  NM_ITEM, "Quit...",     "Q", 0, 0, 0,},
  52.  
  53.     { NM_TITLE, "Edit",      0 , 0, 0, 0,},
  54.     {  NM_ITEM, "Cut",     "X", 0, 0, 0,},
  55.     {  NM_ITEM, "Copy",     "C", 0, 0, 0,},
  56.     {  NM_ITEM, "Paste",     "V", 0, 0, 0,},
  57.     {  NM_ITEM, NM_BARLABEL,  0 , 0, 0, 0,},
  58.     {  NM_ITEM, "Undo",     "Z", 0, 0, 0,},
  59.  
  60.     {   NM_END, 0,          0 , 0, 0, 0,},
  61. };
  62.  
  63. /*------------------------------------------------------------------------*/
  64.  
  65. struct TextAttr customtattr;
  66. struct TextAttr *tattr;
  67.  
  68. /*------------------------------------------------------------------------*/
  69.  
  70. struct Library *GfxBase = NULL;
  71. struct Library *IntuitionBase = NULL;
  72. struct Library *GadToolsBase = NULL;
  73. struct Library *DiskfontBase = NULL;
  74. struct Screen *mysc = NULL;
  75. struct Menu *menu = NULL;
  76. struct Window *mywin = NULL;
  77. struct TextFont *customfont = NULL;
  78. void *vi = NULL;
  79. struct DrawInfo *dri = NULL;
  80. struct Image *checkimage = NULL;
  81. struct Image *amigakeyimage = NULL;
  82.  
  83. /*------------------------------------------------------------------------*/
  84.  
  85. BOOL terminated;
  86.  
  87. /*------------------------------------------------------------------------*/
  88.  
  89. void main(int argc, char **argv)
  90.  
  91. {
  92.     struct IntuiMessage *imsg;
  93.     ULONG imsgClass;
  94.     UWORD imsgCode;
  95.     struct TagItem moretags[3];
  96.  
  97.     terminated = FALSE;
  98.  
  99.     if (argc == 2)
  100.     {
  101.     printf("Usage:\n\tnlmenu\nor\n\tnlmenu fontname.font fontsize\n");
  102.     printf("Example:\n\tnlmenu courier.font 15\n");
  103.     bail_out(0);
  104.     }
  105.     /* Open all libraries: */
  106.  
  107.     if (!(GfxBase = 
  108.     OpenLibrary("graphics.library", 39L)))
  109.     bail_out(20);
  110.  
  111.     if (!(IntuitionBase = 
  112.     OpenLibrary("intuition.library", 39L)))
  113.     bail_out(20);
  114.  
  115.     if (!(GadToolsBase = OpenLibrary("gadtools.library", 39L)))
  116.     bail_out(20);
  117.  
  118.     if (!(DiskfontBase = OpenLibrary("diskfont.library", 37L)))
  119.     bail_out(20);
  120.  
  121.     if (!(mysc = LockPubScreen(NULL)))
  122.     bail_out(20);
  123.  
  124.     if (!(vi = GetVisualInfo(mysc,
  125.     TAG_DONE)))
  126.     bail_out(20);
  127.  
  128.     if (!(dri = GetScreenDrawInfo(mysc)))
  129.     bail_out(20);
  130.  
  131.     if (argc < 3)
  132.     {
  133.     /* Default to screen's font */
  134.     tattr = mysc->Font;
  135.     }
  136.     else
  137.     {
  138.     LONG longval;
  139.  
  140.     customtattr.ta_Style = 0;
  141.     customtattr.ta_Flags = 0;
  142.     /* Attempt to use the font specified on the command line: */
  143.     customtattr.ta_Name = argv[1];
  144.     /* Convert decimal size to long */
  145.     StrToLong(argv[2], &longval);
  146.     customtattr.ta_YSize = longval;
  147.     tattr = &customtattr;
  148.     if (!(customfont = OpenDiskFont(tattr)))
  149.     {
  150.         printf("Could not open font %s %ld\n", customtattr.ta_Name,
  151.         customtattr.ta_YSize);
  152.         bail_out(20);
  153.     }
  154.  
  155.     /* Generate a custom checkmark whose size matches
  156.      * our custom font
  157.      */
  158.     if (!( checkimage = NewObject(NULL, "sysiclass",
  159.         SYSIA_DrawInfo, dri,
  160.         SYSIA_Which, MENUCHECK,
  161.         SYSIA_ReferenceFont, customfont, /* If NULL, uses dri_Font */
  162.         TAG_DONE) ))
  163.     {
  164.         bail_out(20);
  165.     }
  166.  
  167.     /* Generate a custom Amiga-key image whose size matches
  168.      * our custom font
  169.      */
  170.     if (!( amigakeyimage = NewObject(NULL, "sysiclass",
  171.         SYSIA_DrawInfo, dri,
  172.         SYSIA_Which, AMIGAKEY,
  173.         SYSIA_ReferenceFont, customfont, /* If NULL, uses dri_Font */
  174.         TAG_DONE) ))
  175.     {
  176.         bail_out(20);
  177.     }
  178.     }
  179.  
  180.     /* Build and layout menus using the right font: */
  181.     if (!(menu = CreateMenus(mynewmenu,
  182.     TAG_DONE)))
  183.     {
  184.     bail_out(20);
  185.     }
  186.  
  187.     /* These are only necessary if a custom font was supplied... */
  188.     moretags[0].ti_Tag = GTMN_Checkmark;
  189.     moretags[0].ti_Data = (ULONG) checkimage;
  190.     moretags[1].ti_Tag = GTMN_AmigaKey;
  191.     moretags[1].ti_Data = (ULONG) amigakeyimage;
  192.     moretags[2].ti_Tag = TAG_DONE;
  193.  
  194.     if (!LayoutMenus(menu, vi,
  195.     GTMN_TextAttr, tattr,
  196.     GTMN_NewLookMenus, TRUE,
  197.     (customfont ? TAG_MORE : TAG_DONE), moretags))
  198.     bail_out(20);
  199.  
  200.     /* These are only necessary if a custom font was supplied...
  201.      * Note: we re-use some of the tag-array initializations from above
  202.      */
  203.     moretags[0].ti_Tag = WA_Checkmark;
  204.     moretags[1].ti_Tag = WA_AmigaKey;
  205.  
  206.     if (!(mywin = OpenWindowTags(NULL,
  207.     WA_Width, 500,
  208.     WA_InnerHeight, 100,
  209.     WA_Top, 50,
  210.  
  211.     WA_Activate, TRUE,
  212.     WA_DragBar, TRUE,
  213.     WA_DepthGadget, TRUE,
  214.     WA_CloseGadget, TRUE,
  215.     WA_SizeGadget, TRUE,
  216.     WA_SmartRefresh, TRUE,
  217.  
  218.     /* NOTE: NOCAREREFRESH is not allowed if you use GadTools Gadgets! */
  219.     WA_NoCareRefresh, TRUE,
  220.  
  221.     WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_MENUPICK,
  222.  
  223.     WA_MinWidth, 50,
  224.     WA_MinHeight, 50,
  225.     WA_Title, "GadTools Menu Demo",
  226.     WA_NewLookMenus, TRUE,
  227.     (customfont ? TAG_MORE : TAG_DONE), moretags)))
  228.     bail_out(20);
  229.  
  230.     SetMenuStrip(mywin, menu);
  231.  
  232.     while (!terminated)
  233.     {
  234.     Wait (1 << mywin->UserPort->mp_SigBit);
  235.     /* NOTE:  If you use GadTools gadgets, you must use GT_GetIMsg()
  236.      * and GT_ReplyIMsg() instead of GetMsg() and ReplyMsg().
  237.      * Regular GetMsg() and ReplyMsg() are safe if the only part
  238.      * of GadTools you use are menus...
  239.      */
  240.     while ((!terminated) &&
  241.         (imsg = (struct IntuiMessage *)GetMsg(mywin->UserPort)))
  242.     {
  243.         imsgClass = imsg->Class;
  244.         imsgCode = imsg->Code;
  245.         ReplyMsg((struct Message *)imsg);
  246.         switch (imsgClass)
  247.         {
  248.         case IDCMP_MENUPICK:
  249.             terminated = HandleMenuEvent(imsgCode);
  250.             break;
  251.  
  252.         case IDCMP_CLOSEWINDOW:
  253.             printf("CLOSEWINDOW.\n");
  254.             terminated = TRUE;
  255.             break;
  256.         }
  257.     }
  258.     }
  259.     bail_out(0);
  260. }
  261.  
  262. /*------------------------------------------------------------------------*/
  263.  
  264. /*/ bail_out()
  265.  *
  266.  * Function to close down or free any opened or allocated stuff, and then
  267.  * exit.
  268.  *
  269.  */
  270.  
  271. void bail_out(int code)
  272.  
  273.  
  274. {
  275.     if (mywin)
  276.     {
  277.     ClearMenuStrip(mywin);
  278.     CloseWindow(mywin);
  279.     }
  280.  
  281.     /* None of these two calls mind a NULL parameter, so it's not
  282.      * necessary to check for non-NULL before calling.  If we do that,
  283.      * we must be certain that the OpenLibrary() of GadTools succeeded,
  284.      * or else we would be jumping into outer space:
  285.      */
  286.     if (GadToolsBase)
  287.     {
  288.     FreeMenus(menu);
  289.     FreeVisualInfo(vi);
  290.     CloseLibrary(GadToolsBase);
  291.     }
  292.  
  293.     if (dri)
  294.     {
  295.     FreeScreenDrawInfo( mysc, dri );
  296.     }
  297.  
  298.     if (customfont)
  299.     {
  300.     DisposeObject( amigakeyimage );
  301.     DisposeObject( checkimage );
  302.     CloseFont(customfont);
  303.     }
  304.  
  305.     if (mysc)
  306.     {
  307.     UnlockPubScreen(NULL, mysc);
  308.     }
  309.  
  310.     if (DiskfontBase)
  311.     {
  312.     CloseLibrary(DiskfontBase);
  313.     }
  314.  
  315.     if (IntuitionBase)
  316.     {
  317.     CloseLibrary(IntuitionBase);
  318.     }
  319.  
  320.     if (GfxBase)
  321.     {
  322.     CloseLibrary(GfxBase);
  323.     }
  324.  
  325.     exit(code);
  326. }
  327.  
  328.  
  329. /*------------------------------------------------------------------------*/
  330.  
  331. /*/ HandleMenuEvent()
  332.  *
  333.  * This function handles IntuiMessage events of type MENUPICK.
  334.  *
  335.  */
  336.  
  337. BOOL HandleMenuEvent(UWORD code)
  338.  
  339. {
  340.     /* Your code goes here */
  341.     return(FALSE);
  342. }
  343.  
  344. /*------------------------------------------------------------------------*/
  345.